package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.VisibleRangeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.saas.util.UserFieldUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2021/08/09 10:35
 */
@Component
public class OwnerAndCoUserHelp {

    @Resource
    private UserModel userModel;
    @Resource
    private BusinessUserTeamHelp businessUserTeamHelp;

    /**
     * 获取创建人和协同人、负责人，为高级规则可见可编辑做准备
     * @param handlerExplainDTO
     * @param businessType
     * @param paasFormDataEntity
     * @param saasMark
     * @param userMap
     * @param del
     * @throws XbbException
     */
    public JSONObject getOwnerIdAndCoUserId(HandlerExplainDTO handlerExplainDTO, Integer saasMark, Integer businessType,
                                            PaasFormDataEntityExt paasFormDataEntity, Map<String, UserEntity> userMap, Integer del) throws XbbException {
        // 获取协同人和负责人
        Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Long dataId = paasFormDataEntity.getId();
        String ownerId = paasFormDataEntity.getOwnerId();
        Boolean isPublic = false;

        List<FieldAttrEntity> explainList = handlerExplainDTO.getExplainList();
        String creater = null;
        JSONObject data = paasFormDataEntity.getData();
        if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)) {
            creater = data.getString(CustomerManagementEnum.CREATOR.getAttr());
            handlerExplainDTO.setCreatorId(creater);
        } else {
            creater = paasFormDataEntity.getCreatorId();
            handlerExplainDTO.setCreatorId(creater);
        }
        Set<String> userIdList = UserFieldUtil.getUserIdList(explainList, paasFormDataEntity);

        userIdList.add(ownerId);
        userIdList.add(creater);
        List<String> mainUserList = new ArrayList<>();
        List<String> coUserList = new ArrayList<>();
        if (UserTeamEnum.hasTeam(businessType)){
            //产品为可见部门和可见的人特殊的逻辑
            if(IndexTypeEnum.IDX_SAAS_PRODUCT.getCode().equals(businessType)){
                businessUserTeamHelp.getVisableDepAndUser(userIdList,handlerExplainDTO.getCorpid(),dataId,mainUserList,coUserList);
            } else if(IndexTypeEnum.IDX_SAAS_COMPETITOR.getCode().equals(businessType)){
                businessUserTeamHelp.getCompetitorVisableDepAndUser(userIdList,handlerExplainDTO.getCorpid(),dataId,mainUserList,coUserList);
            } else if (IndexTypeEnum.IDX_SERVICE_PROJECT.getCode().equals(businessType)) {
                businessUserTeamHelp.getVisiableDepAndUser4ServiceProject(userIdList, handlerExplainDTO.getCorpid(), dataId, mainUserList, coUserList, del);
            } else {
                if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) && Objects.equals(paasFormDataEntity.getData().getInteger(CustomerManagementEnum.IS_PUBLIC.getAttr()), 1)) {
                    isPublic = true;
                } else if (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode()) && Objects.equals(paasFormDataEntity.getData().getInteger(ClueEnum.IS_PUBLIC.getAttr()), 1)) {
                    isPublic = true;
                }
                businessUserTeamHelp.getUserIdMapSetShow(userIdList,Collections.singletonList(dataId),handlerExplainDTO.getCorpid(),businessType,isPublic,mainUserMap,coUserMap, del);
                mainUserList = mainUserMap.get(dataId);
                coUserList = coUserMap.get(dataId);
            }
        } else if (Objects.equals(XbbRefTypeEnum.INVOICE.getCode(), businessType)) {
            ownerId = paasFormDataEntity.getOwnerId();
            mainUserList.add(ownerId);
            userIdList.add(ownerId);
        } else if((Objects.equals(XbbRefTypeEnum.PURCHASE_INVOICE.getCode(), businessType))){
            ownerId = paasFormDataEntity.getOwnerId();
            mainUserList.add(ownerId);
            userIdList.add(ownerId);
        }else {
            // TODO 现在没有协同人
//            CoUserListDTO coUserListDTO = new CoUserListDTO();
//            coUserListDTO.setCorpid(handlerExplainDTO.getCorpid());
//            coUserListDTO.setFormDataIdIn(Collections.singletonList(dataId));
//            coUserMap = businessUserTeamHelp.coUserMapShow(userIdList,coUserListDTO);
            mainUserList = Collections.singletonList(ownerId);
//            coUserList = coUserMap.get(dataId);
        }
        mainUserList = mainUserList == null ? new ArrayList<>() : mainUserList;
        coUserList = coUserList == null ? new ArrayList<>() : coUserList;
        userIdList.addAll(mainUserList);
        userIdList.addAll(coUserList);
        Map<String, UserEntity>  userEntityMap = userModel.getUserEntityMap(handlerExplainDTO.getCorpid(),new ArrayList<>(userIdList), BasicConstant.ONE);
        userMap.putAll(userEntityMap);
        handlerExplainDTO.setOwnerId(mainUserList);
        handlerExplainDTO.setCoUserId(coUserList);
        JSONObject jsonObject = new JSONObject();
        if (XbbRefTypeEnum.hasVisibleRangeBusiness().contains(businessType)) {
            jsonObject.put(VisibleRangeEnum.VISIBLE_DEPT.getAttr(), mainUserList);
            jsonObject.put(VisibleRangeEnum.VISIBLE_USER.getAttr(), coUserList);
            jsonObject.put(FieldTypeEnum.OWNERID.getAlias(), Collections.singletonList(paasFormDataEntity.getOwnerId()));
            jsonObject.put(FieldTypeEnum.COUSERID.getAlias(), new ArrayList<>());
        }else if(Objects.equals(XbbRefTypeEnum.COMPETITOR.getCode(), businessType)){
            jsonObject.put(CompetitorEnum.VISBLE_DEPT.getAttr(), mainUserList);
            jsonObject.put(CompetitorEnum.VISBLE_USER.getAttr(), coUserList);
            jsonObject.put(FieldTypeEnum.OWNERID.getAlias(), Collections.singletonList(paasFormDataEntity.getOwnerId()));
            jsonObject.put(FieldTypeEnum.COUSERID.getAlias(), new ArrayList<>());
            data.putAll(jsonObject);
        } else {
            jsonObject.put(FieldTypeEnum.OWNERID.getAlias(), mainUserList);
            jsonObject.put(FieldTypeEnum.COUSERID.getAlias(), coUserList);
        }
        return jsonObject;
    }
}
